package com.nd.android.u.ui.widge.chatfragment;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore.MediaColumns;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.common.allCommon.CommonUtils;
import com.common.android.ui.gif.BitmapToolkit;
import com.common.android.ui.widget.SpenEditText;
import com.common.android.utils.audio.AudioRecordOperationManager;
import com.common.android.utils.smiley.Smiley;
import com.common.android.utils.smiley.SmileyView;
import com.common.android.utils.smiley.SmileyView.OnSelectGifListener;
import com.common.android.utils.smiley.Smileyhelper;
import com.common.utils.StarAppUtils;
import com.nd.android.u.allCommonUtils.FileHelper;
import com.nd.android.u.allCommonUtils.ToastUtils;
import com.nd.android.u.controller.ChatConst.MessageContentType;
import com.nd.android.u.controller.MessageConst.ChatNotify;
import com.nd.android.u.controller.factory.ChatBottomFunctionFactory;
import com.nd.android.u.controller.factory.ChatBottomFunctionFactory.ISpecialProccess;
import com.nd.android.u.controller.innerInterface.IBottomFunction;
import com.nd.android.u.controller.innerInterface.IUIDataSupplier;
import com.nd.android.u.ui.R;
import com.nd.android.u.ui.activity.chat_relative_image.SendImageActivity;
import com.nd.android.u.ui.activity.message_chat.ChangeTypefaceCallBack;
import com.nd.android.u.ui.activity.message_chat.ChatAppAdapter;
import com.nd.android.u.ui.chatUiUtils.AudioQuenePlayManager;
import com.nd.android.u.ui.chatUiUtils.SendMessageUtil;
import com.nd.android.u.ui.widge.PopQuickReply;

/**
 * 聊天底部控件 <br>
 * Created 2014-9-20 下午12:16:47
 * 
 * @version
 * @author cb
 * @see
 */
public class BottomFragment extends LinearLayout implements OnClickListener {

	private LinearLayout mchatfootlv;
	/** 发送按钮 */
	private TextView sendbtn;

	/** 录音按钮 */
	private Button btnChatRecord;

	/** 打开表情的按钮 */
	private Button btnOpenSmiley;

	/** 录音或文字按钮图标 */
	private TextView tvSwitcher;
	/** 选择点击弹出框按钮 */
	private Button moreBtn;
	/** 录音按钮 */
	private Button talkBtn;

	/** 选择文件，图片等item */
	private GridView appGrid;
	private ChatAppAdapter chatAppAdapter;
	/** 输入框 */
	private SpenEditText contentEdit;
	/** 表情view */
	private SmileyView mSmileyView;
	/** 相机拍摄保存文件 */
	private Uri mImageUri;
	public ListView mQuickreply;
	public static final int REQUEST_IMAGE_CAPTURE = 2;
	public static final int REQUEST_PHOTO_LIBRARY = 3;
	public static final int REQUEST_PHOTO_SCRAWL = 4;
	public static final int REQUEST_PHOTO_SEND = 5;
	public static final int REQUEST_PHOTO_COLLECTIMAGE = 6;
	public static final int REQUEST_CAPTURE_SEND = 7;
	public static final int REQUEST_FILE = 8;
	public static final String SHOWMENU = "showmenu";// 是否显示菜单
	public static final String EDITTEXTVALUE = "edittextvalue";// 输入框的内容
	/** 通过接口与其它通讯 */
	private CommonBottomListener mcommBottomCallBack;
	/** 数据提供者 */
	private IUIDataSupplier mDataSupplier;
	private boolean mEnableTypeSelection = false;
	/** 当前聊天对象的类型 */
	private int mMessageType;
	/** 上下文 */
	private Activity mActivity;

	private OnBottomDlgChange mOnBottomDlgChange;

	public interface OnBottomDlgChange {

		void onBottomDlgChange();
	}

	public void setOnBottomDlgChange(OnBottomDlgChange onBottomDlgChange) {
		mOnBottomDlgChange = onBottomDlgChange;
	}

	public void notifyBottomDlgChange() {
		if (mOnBottomDlgChange != null) {
			mOnBottomDlgChange.onBottomDlgChange();
		}
	}

	/**
	 * 底部操作监听 <br>
	 * Created 2014-9-20 下午12:17:15
	 * 
	 * @version BottomFragment
	 * @author cb
	 * @see
	 */
	public interface CommonBottomListener {
		/** 底部切换 */
		public void onClickSwitchBtn();

		/**
		 * 构建快捷回复 <br>
		 * Created 2014-9-20 下午12:17:32
		 * 
		 * @author cb
		 */
		public void createPopQuickReply();

		/**
		 * 显示快捷回复 <br>
		 * Created 2014-9-20 下午12:17:41
		 * 
		 * @param width
		 *            长
		 * @param hight
		 *            高
		 * @author cb
		 */
		public void showPopQuickReply(int width, int hight);
	}

	/**
	 * BottomFragment.构造方法 <br>
	 * Created 2014-9-20 下午12:18:00
	 */
	public BottomFragment(Activity activity) {
		super(activity);
		this.mEnableTypeSelection = true;
		mActivity = activity;
		if (activity instanceof CommonBottomListener) {
			mcommBottomCallBack = (CommonBottomListener) activity;
		}
		initView();
		initEvent();
	}

	/**
	 * 设置数据提供者 <br>
	 * Created 2014-9-20 上午11:42:53
	 * 
	 * @param supplier
	 *            数据提供者
	 * @author cb
	 */
	public void setDataSupplier(IUIDataSupplier supplier) {
		mDataSupplier = supplier;
	}

	/**
	 * 设置消息类型 <br>
	 * Created 2014-9-20 上午11:42:46
	 * 
	 * @param type
	 *            消息类型
	 * @author cb
	 */
	public void setMessageType(int type) {
		mMessageType = type;
		initBottomFunction(0);
	}

	/**
	 * 设置群类型（不同的群类型决定不同的底部菜单） <br>
	 * Created 2014-9-20 下午12:32:00
	 * 
	 * @param groupType
	 *            群类型
	 * @author cb
	 */
	public void setGroupType(int groupType) {
		initBottomFunction(groupType);
	}

	/**
	 * 设置界面传过来的字符串 <br>
	 * Created 2014-9-20 下午12:31:27
	 * 
	 * @param editTextvalue
	 *            需要设置到edittext里面的字符串
	 * @author cb
	 */
	public void setEditTextValue(String editTextvalue) {
		contentEdit.setText(editTextvalue);
	}

	/**
	 * 判断是否要自动弹出快捷回复 <br>
	 * Created 2014-8-5 下午3:01:07
	 * 
	 * @author CYP
	 */
	public void isAutoPopQuickReply() {
		int mQuickReplyType;
		mQuickReplyType = mActivity.getIntent().getIntExtra("quick_reply_type",
				PopQuickReply.REPLY_TYPE_CHAT);
		switch (mQuickReplyType) {
		case PopQuickReply.REPLY_TYPE_ACKNOWLEDGE:
			new Handler().postDelayed(new Runnable() {
				public void run() {
					if (mActivity != null && !mActivity.isFinishing()) {
						if (null != mcommBottomCallBack) {
							mcommBottomCallBack.showPopQuickReply(0,
									mchatfootlv.getHeight());
						}
					}
				}
			}, 100);
			break;
		default:
			break;
		}
	}

	/**
	 * 初始化视图
	 * 
	 * @n<b>函数名称</b> :initView
	 * @brief
	 * @see
	 * @version Ver 0.3
	 * @param
	 * @return void
	 * @<b>作者</b> : zhy
	 * @<b>创建时间</b> : 2014-4-4下午3:08:25
	 */
	private void initView() {
		LayoutInflater.from(mActivity).inflate(
				R.layout.chat_pub_num_bottom_fragment, this, true);
		mchatfootlv = (LinearLayout) findViewById(R.id.chat_foot);
		sendbtn = (TextView) findViewById(R.id.tvSend);
		btnChatRecord = (Button) findViewById(R.id.btn_chat_record);
		btnOpenSmiley = (Button) findViewById(R.id.btn_open_smiley);
		StarAppUtils.setViewTypeFace(sendbtn, false);
		contentEdit = (SpenEditText) findViewById(R.id.etChat);
		StarAppUtils.setViewTypeFace(contentEdit, false);
		moreBtn = (Button) findViewById(R.id.btMore);
		moreBtn.setVisibility(mEnableTypeSelection ? View.VISIBLE : View.GONE);

		talkBtn = (Button) findViewById(R.id.btTalk);
		StarAppUtils.setViewTypeFace(talkBtn, false);
		tvSwitcher = (TextView) findViewById(R.id.tvSwitcher);
		appGrid = (GridView) findViewById(R.id.chat_grid_app);
		mQuickreply = (ListView) findViewById(R.id.lvQuickReply);
		mSmileyView = (SmileyView) findViewById(R.id.smileyView);
		mSmileyView.setInputLimited();
		mSmileyView.setParam(contentEdit, Smiley.TYPE_CHAT,
				SmileyView.MAX_INPUT_LEN);
		mSmileyView.showSelection(Smileyhelper.NORMAL);
		mSmileyView.setOnSelectGifListener(listener);
		mSmileyView.setShow(false);
		mSmileyView.hideCatagory(Smileyhelper.COS);
		chatAppAdapter = new ChatAppAdapter(mActivity);
		chatAppAdapter.setChangeTypefaceCallBack(new ChangeTypefaceCallBack() {
			@Override
			public void changeType(TextView view) {
				StarAppUtils.setViewTypeFace(view, false);
			}
		});
	}

	/**
	 * 初始化事件 <br>
	 * Created 2014-9-20 下午12:21:49
	 * 
	 * @author cb
	 */
	private void initEvent() {
		initRecordOperationManager(talkBtn);
		this.setOnClickListener(this);
		sendbtn.setOnClickListener(this);
		tvSwitcher.setOnClickListener(this);
		sendbtn.setOnClickListener(this);
		moreBtn.setOnClickListener(this);
		btnChatRecord.setOnClickListener(this);
		btnOpenSmiley.setOnClickListener(this);
		appGrid.setOnItemClickListener(mOnItemClickListener);
		contentEdit.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					// hideAppGrid();
					hideSmileyViewAndAppGrid();
					// mSmileyView.setVisibility(View.GONE);
					showSoftInput(contentEdit);
					initSendBtnAfterHideDlg();
				}
				return false;
			}
		});

		contentEdit.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				String content = s.toString();
                if(contentEdit.getVisibility() == View.VISIBLE){
                    if (TextUtils.isEmpty(content)) {
                        btnChatRecord.setBackgroundResource(R.drawable.btn_chat_record);
                        btnChatRecord.setVisibility(View.VISIBLE);
                        sendbtn.setVisibility(View.GONE);
                    } else {
                        sendbtn.setVisibility(View.VISIBLE);
                        btnChatRecord.setVisibility(View.GONE);
                    }
                }else{
                    sendbtn.setVisibility(View.VISIBLE);
                    btnChatRecord.setVisibility(View.GONE);
                }
			}
		});
		contentEdit.setText("");
	}

	/**
	 * 初始化底部菜单 <br>
	 * Created 2014-9-20 下午12:37:11
	 * 
	 * @param subType
	 *            子类型
	 * @author cb
	 */
	private void initBottomFunction(int subType) {
		List<IBottomFunction> currentApplist = ChatBottomFunctionFactory
				.getInstance().buildMenuItems(mActivity, mMessageType, subType);
		chatAppAdapter.setIListGridItemList(currentApplist);
		appGrid.setAdapter(chatAppAdapter);
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		// XXX 没有这个按钮功能
		// if (id == R.id.chat_switch_lv) {
		// if (null != mcommBottomCallBack) {
		// mcommBottomCallBack.onClickSwitchBtn();
		// }
		// } else
		if (id == R.id.tvSend) { // 发送按钮
			String data = contentEdit.getText().toString();
			if (TextUtils.isEmpty(data)) {
				if (null != mcommBottomCallBack) {
					mcommBottomCallBack.createPopQuickReply();
				}
				View view = mActivity.getCurrentFocus();
				if (view != null && view instanceof EditText) {
					hideSoftInput((EditText) view);
					new Handler().postDelayed(new Runnable() {
						public void run() {
							if (mActivity != null && !mActivity.isFinishing()) {
								if (null != mcommBottomCallBack) {
									mcommBottomCallBack.showPopQuickReply(0,
											mchatfootlv.getHeight());
								}
							}
						}
					}, 100);
				} else {
					if (null != mcommBottomCallBack) {
						mcommBottomCallBack.showPopQuickReply(0,
								mchatfootlv.getHeight());
					}
				}
				return;
			}
			data = Smileyhelper.getInstance().getEmojiToSend(data);
			SendMessageUtil.sendMessage(MessageContentType.TEXT, data, null, 0,
					mDataSupplier);
			contentEdit.setText("");
		} else if (id == R.id.tvSwitcher || id == R.id.btn_chat_record) {// 语言切换按钮
			// if (appGrid.getVisibility() == View.VISIBLE) {
			// hideAppGrid();
			// } else {
			switchTextOrAudio();
			// }
		} else if (id == R.id.btMore) { // 更多按钮
			hideSoftInput(contentEdit);
			showAppHandler.sendEmptyMessageDelayed(0, 300);
		} else if (id == R.id.btn_open_smiley) {
			openOrHideSmiley();
		}

	}

	/**
	 * 打开或隐藏表情视图
	 * 
	 * <br>
	 * Created 2014-10-21 下午2:24:53
	 * 
	 * @author : HuangYK
	 */
	private void openOrHideSmiley() {
		if (mSmileyView.isShowing()) {
			btnOpenSmiley
					.setBackgroundResource(R.drawable.btn_chat_open_smiley);
			mSmileyView.setShow(false);
			if (contentEdit.getVisibility() == View.VISIBLE) {
				showSoftInput(contentEdit);
				if (TextUtils.isEmpty(contentEdit.getText())) {
					sendbtn.setVisibility(View.GONE);
					btnChatRecord.setVisibility(View.VISIBLE);
					btnChatRecord
							.setBackgroundResource(R.drawable.btn_chat_record);
				} else {
					sendbtn.setVisibility(View.VISIBLE);
					btnChatRecord.setVisibility(View.GONE);
				}
			}
		} else {
			hideAppGrid();
			initSendBtnAfterHideDlg();
			contentEdit.setVisibility(View.VISIBLE);
			talkBtn.setVisibility(View.GONE);

//			if (TextUtils.isEmpty(contentEdit.getText())) {
//				sendbtn.setVisibility(View.GONE);
//				btnChatRecord.setVisibility(View.VISIBLE);
//				btnChatRecord
//						.setBackgroundResource(R.drawable.btn_chat_open_keyboard);
//			} else {
//				sendbtn.setVisibility(View.VISIBLE);
//				btnChatRecord.setVisibility(View.GONE);
//			}
//			btnOpenSmiley
//					.setBackgroundResource(R.drawable.btn_chat_open_keyboard);
			btnOpenSmiley.setBackgroundResource(R.drawable.btn_chat_open_keyboard);
			hideSoftInput(contentEdit);
			mSmileyView.setShow(true);
		}
		invalidate();
		notifyBottomDlgChange();
	}

	// 显示appGridview
	@SuppressLint("HandlerLeak")
	private Handler showAppHandler = new Handler() {
		@TargetApi(Build.VERSION_CODES.CUPCAKE)
		@Override
		public void handleMessage(Message msg) {
			if (appGrid.getVisibility() == View.VISIBLE) {
				hideAppGrid();
				if (contentEdit.getVisibility() == View.VISIBLE) {
					initSendBtnAfterHideDlg();
				}
			} else {
				moreBtn.setBackgroundResource(R.drawable.btn_hide_more);
				notifyBottomDlgChange();
				appGrid.setVisibility(View.VISIBLE);
				btnOpenSmiley
						.setBackgroundResource(R.drawable.btn_chat_open_smiley);
				mSmileyView.setShow(false);

//				if (contentEdit.getVisibility() == View.VISIBLE) {
//					sendbtn.setVisibility(View.GONE);
//					btnChatRecord
//							.setBackgroundResource(R.drawable.btn_chat_open_keyboard);
//					btnChatRecord.setVisibility(View.VISIBLE);
//					talkBtn.setVisibility(View.GONE);
//					tvSwitcher.setCompoundDrawablesWithIntrinsicBounds(
//							R.drawable.chat_icon_audio, 0, 0, 0);
//				} else if (talkBtn.getVisibility() == View.VISIBLE) {
//					contentEdit.setVisibility(View.GONE);
//					btnChatRecord
//							.setBackgroundResource(R.drawable.btn_chat_open_keyboard);
//				}
			}
			notifyBottomDlgChange();
		}
	};

	public void hideAppGrid() {
		if (appGrid != null) {
			appGrid.setVisibility(View.GONE);
			moreBtn.setBackgroundResource(R.drawable.btn_show_more);
		}
	}

	/**
	 * 隐藏弹出框 <br>
	 * Created 2014-9-18 下午7:28:21
	 * 
	 * @return true表示存在弹出框
	 * @author cb
	 */
	public boolean hideSmileyViewAndAppGrid() {
		boolean done = false;
		if (appGrid != null || mSmileyView != null) {
			if (appGrid.getVisibility() == View.VISIBLE) {
				hideAppGrid();
				done = true;
			}
			if (mSmileyView.isShowing()) {
				btnOpenSmiley
						.setBackgroundResource(R.drawable.btn_chat_open_smiley);
				mSmileyView.setShow(false);
				done = true;
			}
			moreBtn.setBackgroundResource(R.drawable.btn_show_more);
		}
		notifyBottomDlgChange();
		return done;
	}

	public boolean appGridOrSmileyViewIsShowing() {
		if (appGrid != null && mSmileyView != null) {
			if (appGrid.getVisibility() == View.VISIBLE
					|| mSmileyView.isShowing()) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 
	 * @n<b>函数名称</b> :switchTextOrAudio
	 * @brief 文本和语音聊天互换
	 * @see
	 * @since Ver 1.1
	 * @param
	 * @return void
	 * @<b>作者</b> : zhy
	 * @<b>创建时间</b> : 2014-4-30下午3:28:18
	 */
	@TargetApi(Build.VERSION_CODES.CUPCAKE)
	public void switchTextOrAudio() {
		// 如果输入框是显示状态的
		if (contentEdit.getVisibility() == View.VISIBLE) {
			if (appGrid.getVisibility() == View.VISIBLE
					|| mSmileyView.isShowing()) {
				// 如果底部的应用对话框或者表情是显示状态的情况
				hideAppGrid();
				mSmileyView.setShow(false);
				showSoftInput(contentEdit);
//				initSendBtnAfterHideDlg();
				btnOpenSmiley
						.setBackgroundResource(R.drawable.btn_chat_open_smiley);
			}
//            else {
				contentEdit.setVisibility(View.GONE);
				talkBtn.setVisibility(View.VISIBLE);
				sendbtn.setVisibility(View.GONE);
				btnChatRecord
						.setBackgroundResource(R.drawable.btn_chat_open_keyboard);
				hideSoftInput(contentEdit);
				tvSwitcher.setCompoundDrawablesWithIntrinsicBounds(
						R.drawable.chat_icon_text, 0, 0, 0);
				btnOpenSmiley
						.setBackgroundResource(R.drawable.btn_chat_open_smiley);
				mSmileyView.setShow(false);
//			}
			postInvalidate();
		} else {
			btnChatRecord.setBackgroundResource(R.drawable.btn_chat_record);
			btnOpenSmiley
					.setBackgroundResource(R.drawable.btn_chat_open_smiley);
			contentEdit.setVisibility(View.VISIBLE);
			talkBtn.setVisibility(View.GONE);
			sendbtn.setVisibility(View.GONE);
            contentEdit.requestFocus();
            contentEdit.setPressed(true);
			showSoftInput(contentEdit);
			if (appGrid.getVisibility() == View.VISIBLE) {
				hideAppGrid();
			}
			if (mSmileyView.isShowing()) {
				mSmileyView.setShow(false);
			}
		}
		notifyBottomDlgChange();
	}

	/**
	 * @n<b>函数名称</b> : 隐藏键盘
	 * @brief
	 * @author lq
	 * @version
	 * @Date 2014-8-20
	 * @param editText
	 * @Return void
	 * @see
	 */
	public void hideSoftInput(EditText editText) {
		try {
			InputMethodManager manager = (InputMethodManager) mActivity
					.getSystemService(Activity.INPUT_METHOD_SERVICE);
			manager.hideSoftInputFromWindow(editText.getWindowToken(), 0);
		} catch (Exception e) {
			Log.d("debug", "hideSoftInput:" + e.getMessage());
		}
	}

	/**
	 * @n<b>函数名称</b> : 显示键盘
	 * @brief
	 * @author lq
	 * @version
	 * @Date 2014-8-20
	 * @param view
	 * @Return void
	 * @see
	 */
	@TargetApi(Build.VERSION_CODES.CUPCAKE)
	private void showSoftInput(View view) {
		try {
			InputMethodManager manager = (InputMethodManager) mActivity
					.getSystemService(Activity.INPUT_METHOD_SERVICE);
			manager.showSoftInput(view, 0);
		} catch (Exception e) {
			Log.d("debug", "showSoftInput:" + e.getMessage());
		}
		notifyBottomDlgChange();
	}

	/**
	 * 在隐藏底部的应用对话框或者表情后，初始化右侧的发送按钮
	 * 
	 * <br>
	 * Created 2014-11-18 下午4:42:21
	 * 
	 * @author : HuangYK
	 */
	private void initSendBtnAfterHideDlg() {
		if (TextUtils.isEmpty(contentEdit.getText())) {
			sendbtn.setVisibility(View.GONE);
			btnChatRecord.setVisibility(View.VISIBLE);
			btnChatRecord.setBackgroundResource(R.drawable.btn_chat_record);
		} else {
			sendbtn.setVisibility(View.VISIBLE);
			btnChatRecord.setVisibility(View.GONE);
		}
	}

	/**
	 * Function: 显示文本等控件
	 * 
	 * @author lq
	 * @version
	 * @Date 2014-8-20
	 * @Return void
	 * @see
	 */
	@TargetApi(Build.VERSION_CODES.CUPCAKE)
	protected void showContentEdit() {
		contentEdit.setVisibility(View.VISIBLE);
		sendbtn.setVisibility(View.VISIBLE);
		talkBtn.setVisibility(View.GONE);
		showSoftInput(contentEdit);
		tvSwitcher.setCompoundDrawablesWithIntrinsicBounds(
				R.drawable.chat_icon_audio, 0, 0, 0);
	}

	// emoji表情按钮监听事件.
	private OnSelectGifListener listener = new OnSelectGifListener() {
		@Override
		public void onSelectGifSmiley(Smiley smiley) {
			if (smiley != null) {
				SendMessageUtil.sendMessage(MessageContentType.PICTURE,
						smiley.id + "", null, 0, mDataSupplier);
			}
		}
	};

	// 聊天应用的单击事件
	protected OnItemClickListener mOnItemClickListener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			IBottomFunction listgriditem = (IBottomFunction) parent
					.getAdapter().getItem(position);
			listgriditem.onClick(mActivity, null, proccess);
		}
	};

	public void showSimplyPopWindow() {
		if (!mSmileyView.isShowing()) {
			hideAppGrid();
			mSmileyView.setShow(true);
		}
		return;
	}

	/**
	 * 处理activity的返回数据 <br>
	 * Created 2014-9-20 下午12:39:46
	 * 
	 * @param requestCode
	 *            requestCode
	 * @param resultCode
	 *            resultCode
	 * @param data
	 *            data
	 * @author cb
	 */
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == Activity.RESULT_OK) {
			hideAppGrid();
			mSmileyView.setShow(false);
			switch (requestCode) {
			case REQUEST_IMAGE_CAPTURE:
				goSendImageActivity(false);
				break;
			case REQUEST_PHOTO_LIBRARY:
				if (data == null){
					return;
				}
				mImageUri = data.getData();
				goSendImageActivity(true);
				break;
			case REQUEST_PHOTO_SCRAWL:
			case REQUEST_CAPTURE_SEND:
			case REQUEST_PHOTO_SEND:
				if (data == null){
					return;
				}
				mImageUri = data.getData();
				File file = getPic(mImageUri);
				if (file == null) {
					ToastUtils.display(mActivity, R.string.chat_no_data_post);
				} else {
					hideAppGrid();
					mSmileyView.setShow(false);
					SendMessageUtil.sendMessage(MessageContentType.PICTURE,
							null, file, 0, mDataSupplier);
				}
				break;
			case REQUEST_PHOTO_COLLECTIMAGE:
				if (data == null){
					return;
				}
				mImageUri = data.getData();
				goCollectImageActivity();
				break;
			case REQUEST_FILE:
				if (data == null){
					return;
				}
				ArrayList<String> checkPaths = new ArrayList<String>();
				checkPaths = data.getStringArrayListExtra("paths");
				if (checkPaths == null || checkPaths.size() == 0) {
					return;
				} else {
					doSendFiles(data);
				}
				break;
			}
		} else if (requestCode == REQUEST_FILE
				&& resultCode == Activity.RESULT_FIRST_USER) {
			if (data == null){
				return;
			}
			ArrayList<String> checkPaths = new ArrayList<String>();
			checkPaths = data.getStringArrayListExtra("paths");
			if (checkPaths == null || checkPaths.size() == 0) {
				return;
			} else {
				doSendFiles(data);
			}
		}
	}

	private void goSendImageActivity(boolean flag) {
		Bundle bundle = new Bundle();
		if (mImageUri == null) {
			File mImageFile;
			try {
				mImageFile = FileHelper.getDefaultImageFile(FileHelper.UPLOAD);
				if (!mImageFile.exists()) {
					return;
				}
				mImageUri = Uri.fromFile(mImageFile);
			} catch (IOException e) {
				e.printStackTrace();
				return;
			}
		}
		bundle.putParcelable("uri", mImageUri);
		bundle.putBoolean("isCapture", !flag);
		Intent intent = new Intent(mActivity, SendImageActivity.class);
		intent.putExtras(bundle);
		if (flag) {
			mActivity.startActivityForResult(intent, REQUEST_PHOTO_SEND);
		} else {
			mActivity.startActivityForResult(intent, REQUEST_CAPTURE_SEND);
		}
	}

	private void goCollectImageActivity() {
		Bundle bundle = new Bundle();
		bundle.putParcelable("uri", mImageUri);
		bundle.putString("collect", "");
		Intent intent = new Intent(mActivity, SendImageActivity.class);
		intent.putExtras(bundle);
		mActivity.startActivity(intent);
	}

	private File getPic(Uri uri) {
		File mFile = null;
		mImageUri = uri;
		String path;
		// 根据URI解析新的图片

		File file = null;
		if (uri.getScheme().equals("content")) {
			path = getRealPathFromURI(mImageUri);
			file = new File(path);
		} else {
			path = mImageUri.getPath();
			file = new File(path);

		}
		if (!CommonUtils.isGifFile(path)) {
			Bitmap resultbitmap = BitmapToolkit.createThumbnailBitmap(
					mActivity, mImageUri, false);
			try {
				FileHelper.saveMyBitmap(file, resultbitmap);
				CommonUtils.recycleBitmap(resultbitmap);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			// 根据统一的命名规则创建新的文件，把原图片文件复制到新文件
			mFile = FileHelper.getDefaultImageFile(FileHelper.UPLOAD);
			if (!mFile.exists()) {
				mFile.createNewFile();
			}
			FileHelper.copyFile(file, mFile);

		} catch (IOException e) {
			e.printStackTrace();
		}
		return mFile;
	}

	private String getRealPathFromURI(Uri contentUri) {
		String[] proj = { MediaColumns.DATA };
		Cursor cursor = mActivity.getContentResolver().query(contentUri, proj,
				null, null, null);
		int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
		cursor.moveToFirst();
		String path = cursor.getString(column_index);
		cursor.close();
		return path;
	}

	/**
	 * 执行批量发送文件
	 * 
	 * @n<b>函数名称</b> :doSendFiles
	 * @brief
	 * @see
	 * @version Ver 1.3.3
	 * @param @param data
	 * @return void
	 * @<b>作者</b> : HuangYK
	 * @<b>创建时间</b> : 2014-1-28下午4:11:47
	 */
	private void doSendFiles(Intent data) {
		ArrayList<String> checkPaths = new ArrayList<String>();
		checkPaths = data.getStringArrayListExtra("paths");
		if (checkPaths == null || checkPaths.size() == 0) {
			return;
		} else {
			for (String path : checkPaths) {
				SendMessageUtil.sendMessage(MessageContentType.FILE, null,
						new File(path), 0, mDataSupplier);
			}
		}
	}

	/**
	 * @n<b>函数名称</b> :initRecordOperationManager
	 * @brief 构造录音控制类
	 * @see
	 * @since Ver 1.2
	 * @param @param talkBtn
	 * @return void
	 * @<b>作者</b> : cb
	 * @<b>创建时间</b> : 2013-7-15下午2:46:23
	 */
	public void initRecordOperationManager(Button talkBtn) {
		new RecordOperationManager(mActivity, talkBtn);
	}

	private class RecordOperationManager extends AudioRecordOperationManager {
		public RecordOperationManager(Context context, Button button) {
			super(context, button);
            setBgRes(R.drawable.btn_touch_record_normal,
                    R.drawable.btn_touch_record_selected);
        }

		/**
		 * @n<b>函数名称</b> :sendAudioMsg
		 * @brief 发送语音文件
		 * @version Ver 1.3
		 * @since
		 * @param @param fileName
		 * @<b>作者</b> : cb
		 * @<b>创建时间</b> : 2013-11-15下午5:08:20
		 */

		@Override
		protected void sendAudioMsg(String fileName, int time) {
			SendMessageUtil.sendMessage(MessageContentType.AUDIO, null,
					new File(fileName), time, mDataSupplier);
		}

		@Override
		protected void recoading() {
			// FRI HYK 请和cb确认一下，正在播放又可以同时录音。
			AudioQuenePlayManager.getInstance().stopPlayAndSetCureentNull();
		}
	}

	// 滑动聊天信息，收回表情菜单、软键盘
	public void dismisswidge() {
		hideSoftInput(contentEdit);
		hideAppGrid();
		initSendBtnAfterHideDlg();
		btnChatRecord.setVisibility(View.VISIBLE);
		if (contentEdit.getVisibility() == View.VISIBLE) {
			sendbtn.setVisibility(View.GONE);
			btnChatRecord.setBackgroundResource(R.drawable.btn_chat_record);
		} else {
			btnChatRecord.setVisibility(View.VISIBLE);
			btnChatRecord.setBackgroundResource(R.drawable.btn_chat_open_keyboard);
		}
		btnOpenSmiley.setBackgroundResource(R.drawable.btn_chat_open_smiley);
		mSmileyView.setShow(false);
	}

	/**
	 * 指定"更多"按钮是否可见, 在初始化时调用 <br>
	 * Created 2014-7-31 下午3:17:41
	 * 
	 * @param enable
	 * @author yufeng
	 */
	public void setEnableTypeSelection(boolean enable) {
		this.mEnableTypeSelection = enable;
	}

	public SpenEditText getContentEdit() {
		return contentEdit;
	}

	ISpecialProccess proccess = new ISpecialProccess() {
		@TargetApi(Build.VERSION_CODES.CUPCAKE)
		@Override
		public Object specialProccess(Object object) {
			if (object instanceof Uri) {
				mImageUri = Uri.parse(((Uri) object).toString());
			} else if (object instanceof Message) {
				int what = ((Message) object).what;
				if (what == ChatNotify.SHOW_SMILEY) {
					showSimplyPopWindow();
					if (contentEdit.getVisibility() == View.GONE) {
						contentEdit.setVisibility(View.VISIBLE);
						sendbtn.setVisibility(View.VISIBLE);
						talkBtn.setVisibility(View.GONE);
						showSoftInput(contentEdit);
						tvSwitcher.setCompoundDrawablesWithIntrinsicBounds(
								R.drawable.chat_icon_text, 0, 0, 0);
					}
				}
			}
			return null;
		}
	};
}
